// pages/login/login.js
//获取应用实例
const app = getApp()
const util = require('../../../utils/util.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loadding : 0,   
    relationTypes: ["2"],
    deviceId: "",
    address: "",
    hideScroll: true,
    timer: 0
  },
  onLoad: function(options) {

  },
  refreshData:function(){
    this.setData({
      deviceId: "",
      address: ""
    })
  },


  bindinput:function(e){
      this.setData({
        address: e.detail.value
      })
  },

  checkboxChange:function(e){
    this.setData({
      relationTypes : e.detail.value,
    })
  },
  getDeviceId: function(e) {
    this.setData({
      deviceId: e.detail.value
    })
  },

  formSubmit: util.throttle(function(e) {
    let waringInfo = ""
    let that = this
    if (this.data.relationTypes.length == 0) {
      waringInfo = "请选择关联类型"
    } else if (this.data.deviceId == "") {
      waringInfo = "请输入蓝牙编号"
    } else if (this.data.deviceId.length != 5) {
      waringInfo = "编号长度不规范"
    }else if (this.data.address == "") {
      waringInfo = "请输入关联网点"
     }else{
       wx.showLoading({
         title: "正在关联"
       })
      let macAddress = "35:"+this.data.deviceId+":"+app.globalData.departmentId
      let address = this.data.address
      wx.request({
        url: app.inspectUrl + '/WeChat/weChatSystem/addAddressRelation', 
        method: 'POST',  
        dataType: 'json',
        data: { 
          schoolId : parseInt(app.globalData.departmentId) ,
          macAddress: macAddress,
          address : address,
          typeList : this.data.relationTypes ,
        },
        success: res => {
          if(res.data == 'success'){
            wx.showToast({
              title: "关联成功"
            }) 
            that.refreshData()
          }else{
            wx.showToast({
              title: "关联失败",
              image: "/images/error.png"
            }) 
            wx.hideLoading()
          }
        },
        fail: () => {
          wx.showToast({
            title: '网络异常！',
            mask: true,
            image: "/images/error.png"
          })
        }
      })
       
    }
    if (waringInfo != "") {
      wx.showToast({
        title: waringInfo,
        image: "/images/warning.png"
      })
    }
    
   
 }),

 addressRelation:function(e){
  wx.navigateTo({
    url: '/subpages/pages/addressRelation/addressRelation?inspectPlanId='
    +this.data.inspectPlanId
  })
}
  
})

